const sky = require('skybase')
let config = require('./config')
const Pack = require('./package.json')
const skyConfig = require('./skyconfig')
const $ = require('meeko')
const SkyDB = require('j2sql2')
const path = require('path')
const sqlite = require('sqlite')
const sqlite3 = require('sqlite3')
const puppeteer = require('puppeteer')
config.beforeMount = async () => {
  // const skyDB = new SkyDB({ mssql: config.mssql}) //可支持sqlserver

  const skyDB = new SkyDB({ mysql: config.mysql, redis: config.redis })
  const db = await skyDB.mysql // 创建mysql实例
  const rd = await skyDB.redis // 创建redis 实例
  global.db = db
  global.redis = rd
  global.rts = await require('skyrts')({
    redis: redis,
    redisAsync: redis,
    gran: '5m, 1h, 1d, 1w, 1M, 1y',
    points: 1000,
    prefix: Pack.name
  })

  /*
  // 连接mysql main实例
  const dbMain = require('j2sql')(config.mysqlMain)
  await $.tools.waitNotEmpty(dbMain, '_mysql')
  global.dbMain = dbMain

  // 连接redis main实例
  const redisMain = createIoredis(config.redisMain)
  await redis.waitForConnected()
  global.redisMain = redisMain

  // 连接mq
  global.MQ = await createRbmq(config.rabbitMQ)

  // 连接kafka
  global.Kafka = await createKafka(config.kafka) */
}
config = Object.assign(skyConfig, config) //将默认config和本地的config合并
async function dbTodayInit () {
  return sqlite.open({
    filename: './data/today.sqlite',
    driver: sqlite3.Database
  })
}
async function dbCheckPwdInit () {
  return sqlite.open({
    filename: './data/pwd_check.sqlite',
    driver: sqlite3.Database
  })
}
async function dbAreaInit () {
  return sqlite.open({
    filename: './data/area.sqlite',
    driver: sqlite3.Database
  })
}
sky.start(config, async () => {
  const browser = await puppeteer.launch({
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--headless',
      '--disable-gpu'
    ]
  })
  const webPage = await browser.newPage()
  global.webPage = webPage
  global.sqliteDB = await dbTodayInit() //全局sqlite
  global.sqliteCheckPwdDB = await dbCheckPwdInit() //全局sqlite
  global.sqliteArea = await dbAreaInit()
  await $.wait(200)
  console.log(global.sqliteDB, process.env.node_port || 13000)
  console.log(
    'http://127.0.0.1:13000/skyapi/sky-stat/getOne?api=_v1_today&type=chart'.fillStr(
      ' ',
      72
    ),
    '某接口5m 1h 1d图形统计'
  )
  console.log(
    'http://127.0.0.1:13000/skyapi/probe/mysql'.fillStr(' ', 72),
    'mysql表结构 Echart'
  )
  console.log(
    'http://127.0.0.1:13000/skyapi/probe/mysqlTree'.fillStr(' ', 72),
    'mysql表树形结构'
  )
  console.log(
    'http://127.0.0.1:13000/skyapi/probe/mysqlGrid'.fillStr(' ', 72),
    'mysql表-列明细'
  )
  console.log(
    'http://127.0.0.1:13000/skyapi/mock/first'.fillStr(' ', 72),
    '简单mock接口'
  )
  console.log(
    'http://127.0.0.1:13000/skyapi/mock/captcha'.fillStr(' ', 72),
    '验证码图像返回接口'
  )
  console.log(
    'http://127.0.0.1:13000/company/getName?key=xonfwb&getType=ali&name=腾讯计算机系统',
    'Ali企业信息查询'
  )
  console.log('http://127.0.0.1:13000/xin/tapd', 'TAPD聚合看板')
})
